#ifndef __MESSAGE_HPP__
#define __MESSAGE_HPP__

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <string>

#include "Block.hpp"


/**
 * implements an arbitrarily long message that may span several Blocks in size
 * and provides functions to split the messages in multiple Blocks
 */
class Message {

	public:
		/**
		 * initializes empty message with block size = 16
		 */
		Message();

		/**
		 * initializes empty message
		 * @param BlockSize block size to be used in bytes
		 */
		Message(unsigned int BlockSize);

		virtual ~Message();

		/**
		 * clears internal buffers and deletes message data
		 */
		void clear();

		/**
		 * prepares internal buffer to contain data of certain length
		 * internally, buffer is extended to a multiple of buffer size
		 * @param size in bytes
		 */
		bool prepare(unsigned int size);

		/**
		 * reads contents from given file into buffer
		 * @param filename
		 */
		void readFromFile(const std::string &filename);
		
		/**
		 * writes contents of buffer to given file
		 * @param filename
		 */
		void writeToFile(const std::string &filename) const;

		/**
		 * @returns size of buffer
		 */
		unsigned int getSize() const;

		/**
		 * @returns number of bytes that were padded to obtain a size of n*blockSize
		 */
		unsigned int getPaddedSize() const;

		/**
		 * @returns number of blocks that represent buffer
		 */
		unsigned int getBlockCount() const;

		/**
		 * @returns block with given index, starting at 0
		 */
		Block getBlock(unsigned int number) const;

		/**
		 * sets block with given index, starting at index 0
		 * @param number index of block
		 * @param content block content that is written in internal buffer
		 */
		void setBlock(unsigned int number, const Block &content);


	private:
		unsigned char *data;
		unsigned int length;
		unsigned int paddedLength;
		unsigned int blockSize;

		unsigned int getFilesize(const std::string &Filename);
};
#endif

